# 导包
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select

# 打开浏览器
service = Service(r"D://Python314//chromedriver.exe")
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service, options=chrome_options)   # 创建浏览器对象


# 2.输入访问地址（URL）
driver.get("http://121.43.169.97:8848/pageA.html")
# 放大浏览器页面
driver.maximize_window()

'''
# 步骤:
# 1. 导包 Select类 --> from selenium.webdriver.support.select import Select
# 2. 实例化Select类
#     匿名: Select(element).select_by_index()  #下标/索引
#     实名:  select = Select(element)
#           select.select_by_index()

# 注意: 
    # 1. Select类是通过select标签来控制其下的option元素
    # 2. element: 只能是select标签
'''

# ---------------------------------------------------# 下标/索引------------------------------------------------
'''
# 1).选择‘广州’ 
el = driver.find_element_by_css_selector("#selectA")
el = driver.find_element(By.CSS_SELECTOR,"#selectA")
select = Select(el)
sleep(2)
select.select_by_index(2)
# 2).暂停2秒，选择‘上海’
sleep(2)
select.select_by_index(1)
# 3).暂停2秒，选择‘北京’
sleep(2)
select.select_by_index(0)
'''
# ---------------------------------------------------# value值实现 ------------------------------------------------

'''
el = driver.find_element_by_css_selector("#selectA")
el = driver.find_element(By.CSS_SELECTOR,"#selectA")
select = Select(el)
sleep(2)
# 1).选择‘广州’
select.select_by_value("gz")
# 2).暂停2秒，选择‘上海’
sleep(2)
select.select_by_value("sh")
# 3).暂停2秒，选择‘北京’
sleep(2)
select.select_by_value("bj")
'''

# ---------------------------------------------------# 文本值显示内容  ------------------------------------------------

# el = driver.find_element_by_css_selector("#selectA")
el = driver.find_element(By.CSS_SELECTOR,"#selectA")
select = Select(el)
time.sleep(2)
# 1).选择‘广州’
select.select_by_visible_text("广州")
# 2).暂停2秒，选择‘上海’
time.sleep(2)
select.select_by_visible_text("上海")
# 3).暂停2秒，选择‘北京’
time.sleep(2)
select.select_by_visible_text("北京")





# sleep(3)
# driver.quit()